import { Component, OnInit, Input } from '@angular/core';
import { PopoverController, ModalController, AlertController } from '@ionic/angular';
import { PageModalService } from 'src/app/shared/service/page-modal-service';
import { HttpService } from 'src/app/shared/service/http.service';
import { SharePageComponent } from '../share-page/share-page.component';
@Component({
  selector: 'app-popover',
  templateUrl: './popover.component.html',
  styleUrls: ['./popover.component.scss'],
})
export class PopoverComponent implements OnInit {

  @Input() tagData;
  shareData: any;
  constructor(
    public popoverController: PopoverController,
    public modalController: ModalController,
    public pageModalService: PageModalService,
    public http: HttpService,
    public alertController: AlertController,
  ) { }

  ngOnInit() {
    // console.log('----->', this.tagData);
  }
  async dismissPopover() {
    let currentPopover = this.popoverController.getTop();
    if (currentPopover) {
      (await currentPopover).dismiss().then(() => {
        currentPopover = null;
      });
    }
  }

  // 跳转页面
  operation(item) {
    this.dismissPopover(); // 关闭小弹窗
    if (item.action === "share") {
      this.share(item.link);
    } else if (item.link && item.link.type === 'event') {
      if (item.link.params && item.link.params.eventName) {
        this.pageModalService.eventService.eventEmit.emit(item.link.params.eventName,item.link);
      } else {
        this.pageModalService.openPage(item.link);
      }
    } else if (item.link && item.link.type === 'refreshCurrentPage') {
      this.pageModalService.appGlobalService.reloadCurrentPage();
    } else if (item.link && item.link.type === 'questionCall') {
      this.call(item.link.params);
    } else if (item.action === "submit"){
      const params = {
        content: item.submit.content,
        id: item.submit.id,
        sessionId: localStorage.getItem("sessionId")
      };
      this.http.post("application/submit", params, res => {

        if (res.success) {
          if (res.data.eventResult !== undefined) {
            let temporaryImg;
            temporaryImg = item.submit.content.actionIcon;
            item.submit.content.actionIcon = item.submit.content.img;
            item.submit.content.img = temporaryImg;
            if (item.label === '收藏') {
              item.label = '已收藏';
            } else {
              item.label = '收藏';
            }
          }
        }
      });
    } else {
      this.pageModalService.openPage(item.link);
    }


  }

  /** 分享 */
  share(link) {
    console.log('share --> page', link);
    // 请求参数
    const params = {
      sessionId: localStorage.getItem("sessionId"),
      pageId: link.target,
      params: link.params
    };
    this.http.post('application/page', params, res => {
      console.log(JSON.parse(res.script));
      this.shareData = JSON.parse(res.script).views[0].tags[0].data;
      // console.log(this.shareData);
      this.presentModal();
    });
  }

  // 分享弹框
  async presentModal() {
    const modal = await this.modalController.create({
      showBackdrop: true,
      cssClass: 'sharePage',
      backdropDismiss: true,
      component: SharePageComponent,
      componentProps: { tagData: this.shareData }
    });
    await modal.present();
    // 监听销毁的事件
    const modalData = await modal.onDidDismiss(); // 获取关闭传回的值
    console.log(modalData);
  }

  async call(item) {
    const alert = await this.alertController.create({
      header: '',
      message: item.question,
      buttons: [
        {
          text: '取消',
          role: 'cancel',
          cssClass: 'secondary'
        }, {
          text: '拨打',
          handler: () => {
            this.http.changeCallNumber(item.phone);
          }
        }
      ]
    });
    await alert.present();
  }
}
